﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.AP.APBill;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// APBillRevocateSV partial 
    /// </summary>	
	public partial class APBillRevocateSV 
	{	
		internal BaseStrategy Select()
		{
			return new APBillRevocateSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class APBillRevocateSVImpementStrategy : BaseStrategy
	{
		public APBillRevocateSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			APBillRevocateSV bpObj = (APBillRevocateSV)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<DocDTOData> retlist = new List<DocDTOData>();

                //List<object> inputdtos = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item => item as object)
                //	, typeof(CompRptKeyDTOData));
                foreach (var dto in bpObj.Data)
                {
                    APBillHead entity = APBillHead.Finder.Find($"DocNo='{dto.DocNo}'");
                    if (entity == null)
                    {
                        //新增逻辑
                        throw new Exception($"单号:{dto.DocNo}不存在!");

                    }
                    else
                    {
                        if (entity.DocStatus != UFIDA.U9.CBO.FI.Enums.BillStatusEnum.Approving)
                            throw new Exception($"单号:{entity.DocNo}不为提交,不能收回!");

                        //修改逻辑
                        using (ISession session = Session.Open())
                        {
                            entity.DocStatus = UFIDA.U9.CBO.FI.Enums.BillStatusEnum.Opened;
                            session.Commit();
                        }

                        DocDTOData retdto = new DocDTOData()
                        {
                            DocNo = entity.DocNo,
                            ID = entity.ID.ToString(),
                        };

                        retlist.Add(retdto);
                    }
                }
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }		
	}

	#endregion
	
	
}